﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using RuTrackerLib.ServiceAttibutes;

namespace RuTrackerLib.Editors.Links
{
    [PerApplicationService]
    public class LinksLexer
    {
        private const string LinksSearchPattern = "\\<a.*?href=\".*?\".*?(\\</a\\>|/\\>)";

        public IList<Link> Parse(string html)
        {
            Regex regex = new Regex(LinksSearchPattern);
            MatchCollection collection = regex.Matches(html);

            List<Link> result = new List<Link>();
            foreach (Match match in collection)
            {
                try
                {
                    result.Add(new Link(match.Value));
                }
                catch(ArgumentException){ }
            }
             
            return result;
        }
    }
}
